package com.itheima.gamedemo;

import java.util.*;

public class Room {

    //1.准备好54张牌给房间使用，放到一个集合中
    private List<Card> allCards = new ArrayList<>();

    //2.初始化54张牌（使用实例代码块）
    {
        //3.准备点数
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //4.准备花色
        String[] colors = {"♠", "♠", "♣", "♦"};

        //5.组合点数和花色成为牌对象，放到cards集合中
        int size = 0;       //牌大小（认为点数为3的大小是1，点数为4的大小是2，以此类推）
        /*for (String color : colors) {
                size++;     //错误排序
            for (String number : numbers) {
                Card card = new Card(color, number, size);
                allCards.add(card);
            }
        }*/
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                Card card = new Card(color, number, size);
                allCards.add(card);
            }
        }

        //6.添加大小王
        /*allCards.add(new Card("", "小王"));
        allCards.add(new Card("", "大王"));*/
        Collections.addAll(allCards, new Card("", "小王",++size), new Card("", "大王",++size));

        System.out.println("新牌：" + allCards);

    }
    public void startGame() {

        //7.洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后：" + allCards);

        //8.发牌，玩家1 =【】，玩家2 =【】，玩家3 =【】，底牌 =【】
        Map<String, List<Card>> players = new HashMap<>();

        List<Card> playerCards1 = new ArrayList<>();
        players.put("玩家1", playerCards1);

        List<Card> playerCards2 = new ArrayList<>();
        players.put("玩家2", playerCards2);

        List<Card> playerCards3 = new ArrayList<>();
        players.put("玩家3", playerCards3);

        List<Card> leftCards = new ArrayList<>();

        //9.遍历allCards集合，把51张牌发给三个玩家，剩余的3张牌给底牌
        for (int i = 0; i < allCards.size(); i++) {
            Card card = allCards.get(i);
            if (i >= 51) {
                leftCards.add(card);
            } else {
                if (i % 3 == 0) {
                    playerCards1.add(card);
                } else if (i % 3 == 1) {
                    playerCards2.add(card);
                } else {
                    playerCards3.add(card);
                }
            }
        }

        //10.看底牌
        System.out.println("底牌：" + leftCards);

        //11.把底牌随机给一个玩家
        playerCards1.addAll(leftCards);

        //12.给各个玩家的牌排序（按点数从小到大）
        sortCards(playerCards1);
        sortCards(playerCards2);
        sortCards(playerCards3);

        //13.看牌，遍历Map集合
        for (Map.Entry<String, List<Card>> entry : players.entrySet()) {

            String name = entry.getKey();               //玩家名称
            List<Card> cards = entry.getValue();        //玩家的牌
            System.out.println(name + "的牌：" + cards);

        }

    }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize() - o2.getSize();
            }
        });
    }
}
